package com.link.base.base.appmenuurls.utils;

import com.link.base.base.appduty.model.AppMenu;
import com.link.base.base.appduty.service.AppMenuService;
import com.link.base.base.appmenuurls.model.AppMenuUrls;
import com.link.base.base.appmenuurls.service.AppMenuUrlsService;
import com.link.base.base.pageurls.model.PageUrls;
import com.link.base.base.pageurls.service.PageUrlsService;
import com.link.base.enterprise.model.CoreEnterprise;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.interceptor.SecurityInterceptor;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.BeanTools;
import com.link.core.util.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/***
 * 增加页面url的线程类
 *
 * @author LiYang
 * @date 2019-12-16
 **/
public class RunAddAppPageUrl implements Runnable {

    private String token;
    private CoreUser user;
    private CoreEnterprise enterprise;


    public RunAddAppPageUrl(String token, CoreUser user, CoreEnterprise enterprise) {
        this.token = token;
        this.user = user;
        this.enterprise = enterprise;
    }

    @Override
    public void run() {
        try {
            SecurityInterceptor.token.set(token);
            SecurityInterceptor.tempUser.set(user);
            SecurityInterceptor.tempEnterpirse.set(enterprise);
            AppMenuService appMenuService = (AppMenuService) BeanTools.getBean("appMenuServiceImpl");
            PageUrlsService pageUrlsService = (PageUrlsService) BeanTools.getBean("pageUrlsServiceImpl");
            AppMenuUrlsService appMenuUrlsService = (AppMenuUrlsService) BeanTools.getBean("appMenuUrlsServiceImpl");
            CoreEnterprise enterprise = SecurityInterceptor.tempEnterpirse.get();
            String industryVersion = enterprise.getIndustryVersion();
            if (StringUtils.isBlank(industryVersion)) {
                throw new ServiceException("APPDUTY-001");
            }
            QueryParams qpsTemp = new QueryParams();
            qpsTemp.setPageFlag(false);
            AppMenu t = (AppMenu) BasicModel.transformClass(AppMenu.class, qpsTemp);
            Field f = t.getClass().getDeclaredField(industryVersion);
            f.setAccessible(true);
            f.set(t, "Y");
            List<AppMenu> list = appMenuService.queryByExamplePage(t);
            for (AppMenu appMenu : list) {
                List<String> menuUrl = new ArrayList<>();
                save(menuUrl,appMenu, pageUrlsService, appMenuUrlsService);
            }
        } catch (Exception e) {
            throw new BasicServiceException("增加页面url的线程类错误" + e.getMessage());
        }
    }

    private void save(List<String> menuUrl,AppMenu appMenu, PageUrlsService pageUrlsService, AppMenuUrlsService appMenuUrlsService) throws Exception {
        if (!menuUrl.contains(appMenu.getUrlPath())) {
            PageUrls pageUrls = new PageUrls();
            pageUrls.setPagePath(appMenu.getUrlPath());
            pageUrls.setSystem("APP");
            pageUrls.setAttr1(appMenu.getId().toString());
            List<PageUrls> pageUrlsTemp = pageUrlsService.queryByPagePath(pageUrls);
            if (pageUrlsTemp == null || pageUrlsTemp.size() <= 0) {
                return;
            }
            List<AppMenuUrls> menuPageUrls = new ArrayList<>();
            for (PageUrls pageUrls1 : pageUrlsTemp) {
                AppMenuUrls menuPageUrls1 = new AppMenuUrls();
                menuPageUrls1.setPageUrlId(pageUrls1.getId());
                menuPageUrls1.setMenuId(appMenu.getId());
                menuPageUrls1.setEffective(pageUrls1.getEffective());
                menuPageUrls1.setUrlType(pageUrls1.getUrlType());
                menuPageUrls1.setUrl(pageUrls1.getUrl());
                menuPageUrls1.setPagePath(pageUrls1.getPagePath());
                menuPageUrls1.setPushPage(pageUrls1.getPushPage());
                menuPageUrls1.setRemarks(pageUrls1.getRemarks());
                menuPageUrls1.setSystem(pageUrls1.getSystem());
                menuPageUrls.add(menuPageUrls1);
            }
            appMenuUrlsService.batchInsert(menuPageUrls);
            String pagePath = pageUrlsTemp.get(0).getPushPage();
            if (StringUtils.isBlank(pagePath)) {
                return;
            }
            menuUrl.add(pagePath);
        }
    }
}
